package com.liit.controller.weixin.controller;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.liit.weixin.core.basic.jsapi.JsapiTicketSupport;
import cn.liit.weixin.core.config.Config;
import cn.liit.weixin.jssdk.basic.JssdkConfig;

import com.liit.annotation.SystemLog;
import com.liit.controller.index.BaseController;
import com.liit.entity.ProvinceFormMap;
import com.liit.entity.WeixinUserFormMap;
import com.liit.mapper.WeixinUserMapper;
import com.liit.util.Common;

/**
 * @author dingweibo
 * @Title: WeixinUserController.java
 * @Description: TODO(用一句话描述该文件做什么)
 * @date 2016年1月26日
 */
@Controller
@RequestMapping("/weixin/")
public class WeixinController extends BaseController {

	@Inject
	private WeixinUserMapper weixinUserMapper;

	@ResponseBody
	@RequestMapping("save")
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	@SystemLog(module = "功能管理", methods = "手机微信-注册用户")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String saveEntity() throws Exception {
		WeixinUserFormMap weixinUserFormMap = getFormMap(WeixinUserFormMap.class);
		weixinUserMapper.editEntity(weixinUserFormMap);
		return "success";
	}

	/**
	 * 微信平台注册
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("reg")
	public String weixinReg(Model model) throws Exception {
		String openid = getPara("openid");
		WeixinUserFormMap weixinUser = weixinUserMapper.findbyFrist("openid", openid, WeixinUserFormMap.class);
		if (weixinUser == null) {
			throw new NullPointerException("openid is null");
		}
		model.addAttribute("weixinUser", weixinUser);
		model.addAttribute("province", weixinUserMapper.findByWhere(new ProvinceFormMap()));
		model.addAttribute("jssdk", JssdkConfig.config(Config.notifyurl + "/weixin/reg.shtml?openid=" + openid, JsapiTicketSupport.getJsapi_ticket()));
		return Common.BACKGROUND_PATH + "/weixin/reg";
	}
}
